package com.clkj.cldisabledmanclient.mvp.myasset;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.jaylv.JayLvBaseAdapter;
import com.clkj.cldisabledmanclient.adapter.jaylv.ViewHolder;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseFragment;
import com.clkj.cldisabledmanclient.bean.MyAssetBean;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.constant.TipStr;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.LogUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/6/20.
 */

public class FragmentMyPeriodAsset extends BaseFragment implements MyPeriodAssetsContract.View, PullToRefreshBase.OnRefreshListener2 {


    //默认的是isFirstVisibles true ,刚进来的时候分两种情况
    //1.如果切过来的时候 setVisibleHint在OnCreate之前调用，那isFirstVisible为true，这个时候不进行任何操作；
    //等方法执行到onActivityCreated的时候，在执行一次setUserVisibleHint 这个时候把默认的是isFirstVisibles 设为false
    //然后这个时候调用setUserVISIBLEhINT为true  去获取数据
    //2.如果是后面再次切过来，那isFirstVisible已经为false，那就可以直接通过setUserVisibleHint获取数据了

    //代表这个界面是不是第一次被使用
    boolean isFirstUsed = true;
    boolean isGetData = false;
    //是否显示过子fragment
    boolean isShowSonFragment = false;

    public static final String NEWSKIND = "newstype";
    Integer mPageIndex;
    boolean isRefresh;
    boolean canLoad;


    private List<MyAssetBean> myAssetBeanList = new ArrayList<>();
    private JayLvBaseAdapter<MyAssetBean> mAdapter;

    private Context mContext;
    private PullToRefreshListView mLvPeriodAssets;
    private RelativeLayout mRlEmpty;
    private TextView mTvTipEmpty;

    private String idCard;
    private MyPeriodAssetsContract.Presenter mPresenter;

    public static FragmentMyPeriodAsset newInstance() {
        Bundle bundle = new Bundle();
        //bundle.putSerializable(NEWSKIND, newsType);
        FragmentMyPeriodAsset fragment = new FragmentMyPeriodAsset();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            //mNewsType = (NewsTypeBean) bundle.getSerializable(NEWSKIND);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_period_assets, container, false);
        initData();
        assignView(view);
        initView();

        //tvContent.setText(newsType);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //fragment 可见的情况下 进行加载数据。不可见则不加载
        if (getUserVisibleHint()) {
            if (isFirstUsed) {
                isFirstUsed = false;
                setUserVisibleHint(true);
            }
            //不可见，说明fragment已经初始话完成,就直接把isFirstVisible改为false 再次切过来的时候就直接可以加载数据了
        } else {
            isFirstUsed = false;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // LogUtil.e("CartVisible");
        if (getUserVisibleHint()) {
            LogUtil.e("MyShuoShuoVisible");
            //如果不是firstvisible才去进行下面的操作  因为第一次很可能出现各种问题
            if (!isFirstUsed) {
                if (!isGetData) {
                    getOriginData();
                }
            }
        } else {
            LogUtil.e("faXianFragmentVisible");
        }
    }

    @Override
    public void initData() {
        mPageIndex = 1;
        canLoad = true;
        mContext = getActivity();

        idCard = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        mPresenter = new MyPeriodAssetsPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

    }

    //获取初始数据
    void getOriginData() {

        getMyPeriodAssetListList();
    }


    @Override
    public void assignView(View view) {

        mLvPeriodAssets = view.findViewById(R.id.lv_period_assets);
        mRlEmpty = view.findViewById(R.id.rl_empty);
        mTvTipEmpty = view.findViewById(R.id.tv_tip_empty);
    }

    @Override
    public void initView() {

        mAdapter = new JayLvBaseAdapter<MyAssetBean>(getActivity(), myAssetBeanList, R.layout.item_my_recover_plan) {
            @Override
            public void convert(ViewHolder helper, int position, MyAssetBean item) {

                //helper.setText(R.id.tv_orgName,)
            }
        };
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mContext = null;
    }

    @Override
    public void setPresenter(MyPeriodAssetsContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {

        showProgressDialog(true, null);
    }

    @Override
    public void dismissLoading() {

        dismissProgressDialog();
    }

    @Override
    public void getMyPeriodAssetListList() {
        mPresenter.getMyPeriodAssetList(idCard, mPageIndex.toString(),
                Config.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void getMyPeriodAssetListSuccess(List<MyAssetBean> myAssetBeans) {

        mLvPeriodAssets.onRefreshComplete();

        if (isRefresh) {
            myAssetBeanList.clear();
            isRefresh = false;
        }

        if (myAssetBeans.size() > 0) {

            if (myAssetBeans.size() >= Config.DEFAULT_PAGE_SIZE) {
                mLvPeriodAssets.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                mLvPeriodAssets.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            myAssetBeanList.addAll(myAssetBeans);

        } else {
            canLoad = false;
            mLvPeriodAssets.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (myAssetBeanList.size() == 0) {
                mLvPeriodAssets.setEmptyView(mRlEmpty);
                mTvTipEmpty.setText("未查询到您的康复计划数据");
            } else {
                ToastUtil.showShort(getActivity(), TipStr.TIP_NOT_MORE_DATAS);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getMyPeriodAssetListError(String tip) {

        mLvPeriodAssets.onRefreshComplete();
        if (isRefresh) {
            myAssetBeanList.clear();
            isRefresh = false;
        }
        ToastUtil.showShort(getActivity(), tip);
        canLoad = false;
        mLvPeriodAssets.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        if (myAssetBeanList.size() == 0) {
            mLvPeriodAssets.setEmptyView(mRlEmpty);
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex = 1;
        getMyPeriodAssetListList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex++;
        getMyPeriodAssetListList();
    }
}
